# coding=utf-8
"""
每日8点
cron: 0 0 8 * * ?
new Env("药研社")
"""

import requests
import notify
import utils.tool as TOOL
import utils.getCookie as getCookie

global msg

msg = """
名称|今日|总研值
:--:|:--:|:--:
"""


def main():
    users = getCookie.GetQlEnvs().getCookies('YYS_TOKEN')
    print("🎉🎉🎉🎉🎉🎉🎉🎉🎉🎉🎉🎉🎉🎉🎉🎉\n")
    print("获取到: "+str(len(users))+" 个用户")
    print("-------------------------------------------")

    for user in users:
        signIn(user)


# 签到
def signIn(user):
    global msg
    url = "https://m.yaoyanshe.com/web-api/mdUser/v2/sign"
    headers = {
        "Host": "m.yaoyanshe.com",
        "Connection": "keep-alive",
        "accessToken": user['cookie'],
        "app-key": "MODULE_YYS_H5_201903161",
        "version": "3.9.4",
        "app-type": "",
        "User-Agent": "Mozilla/5.0 (Linux; Android 11; Redmi K20 Pro Premium Edition Build/RKQ1.200826.002; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/87.0.4280.141 Mobile Safari/537.36",
        "Accept": "application/json, text/plain, */*",
        "sign": "16892536F31504A9874AE85846B741D6",
        "product-code": "yys-h5",
        "devcie": "iOS",
        "app-version": "3.9.4",
        "X-Requested-With": "com.healthmudi.dia",
        "Sec-Fetch-Site": "same-origin",
        "Sec-Fetch-Mode": "cors",
        "Sec-Fetch-Dest": "empty",
        "Referer": "https://m.yaoyanshe.com/h/fs/sign_in",
        "Accept-Language": "zh-CN,zh;q=0.9,en-US;q=0.8,en;q=0.7"
    }
    res = requests.get(url=url, headers= headers)
    json = res.json()

    if json['status'] != 0:
        print(user['name'] + " ---> " + "今日签到研值: " +
              str(json['subMessage']) + "  总研值: " + str(json['subMessage']))
        msg += user['name'] + "|" + "失效" + "|" + "失效" + "\n\n"
        return

    oneDayReward = json['data']['oneDayReward']
    # 签到成功 获取 药研值
    url = "https://yys.yaoyanshe.com/app-api/mdUserPoints/getUserPoints"
    res = requests.get(url=url, headers=headers)
    json = res.json()

    allDayReward = json['data']

    print(user['name'] + " ---> " + "今日签到研值: " +
          str(oneDayReward) + "  总研值: " + str(allDayReward))

    print("-------------------------------------------")

    msg += user['name'] + "|" + \
        str(oneDayReward)+"|"+str(allDayReward)+"\n\n"


main()


print("\n🎉🎉🎉🎉🎉🎉🎉🎉🎉🎉🎉🎉🎉🎉🎉🎉")


notify.send("药研社", msg)

